﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TravelAgencyDAL;
using System.Threading;
using System.Globalization;

namespace Beginning
{
    public partial class frmBusesSchedules : Form
    {
        public frmBusesSchedules()
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("en-US");
            InitializeComponent();
            BusesBindControls();
            DestinationsBindControls();
            StartHoursBindControls();
            BusesForStartHourBindControls();
            DatesForBusBindControls(); 
        }

        private void BusesBindControls()
        {
            var buses = from b in DAO.GetAllBuses()
                        select b.BusRegNumber;

            cbBuses.DataSource = buses.ToList();
        }

        private void DestinationsBindControls()
        {
            var destinations = from d in DAO.GetAllDestinations()
                               select d.DestistionName;

            cbDestinations.DataSource = destinations.ToList();
        }

        private void StartHoursBindControls()
        {
            string selectedDestination = cbDestinations.SelectedItem.ToString();
            var currentID = from d in DAO.GetAllDestinations()
                            where d.DestistionName == selectedDestination
                            select d.DestinationID;

            List<int> startHoursList = DAO.GetAllStartHoursForDestination(currentID.First());
            List<string> startHour = new List<string>();

            foreach (var sh in startHoursList)
            {
                startHour.Add((sh / 60).ToString() + ":" + (sh % 60).ToString());
            }

            lbStartHours.DataSource = startHour.ToList();
        }

        private void BusesForStartHourBindControls()
        {
            string selectedDestination = cbDestinations.SelectedItem.ToString();
            var currentID = (from d in DAO.GetAllDestinations()
                            where d.DestistionName == selectedDestination
                            select d.DestinationID).First();

            lbBusesInService.DataSource = DAO.GetBusesForStartHour(currentID, lbStartHours.SelectedValue.ToString());
        }

        private void DatesForBusBindControls()
        {
            string selectedDestination = cbDestinations.SelectedItem.ToString();
            var currentID = (from d in DAO.GetAllDestinations()
                             where d.DestistionName == selectedDestination
                             select d.DestinationID).First();

            if (lbBusesInService.SelectedValue != null)
            {
                lbDates.DataSource = DAO.GetDatesForBus(currentID, lbStartHours.SelectedValue.ToString(),
                    lbBusesInService.SelectedValue.ToString());
            }
            else
            {
                lbDates.DataSource = null;
            }
        }

        private void btnAttachBus_Click(object sender, EventArgs e)
        {
            BusShedule busSchedule = new BusShedule();

            string busNumber = cbBuses.SelectedItem.ToString();
            var selectedBusID = (from b in DAO.GetAllBuses()
                                 where b.BusRegNumber == busNumber
                                 select b.BusID).First();

            string selectedDestination = cbDestinations.SelectedItem.ToString();
            var currentID = from d in DAO.GetAllDestinations()
                            where d.DestistionName == selectedDestination
                            select d.DestinationID;

            var selectedStartHourID = (from s in DAO.GetAllStartHoursForDest(currentID.First())
                                       where ((s.StartHourValue / 60) + ":" + (s.StartHourValue % 60)).ToString()
                                       == lbStartHours.Text
                                       select s.StartHourID).First();

            string[] hoursAndMinutes = lbStartHours.Text.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
            int hours = Convert.ToInt32(hoursAndMinutes[0]);
            int minutes = Convert.ToInt32(hoursAndMinutes[1]);

            busSchedule.BusID = selectedBusID;
            busSchedule.StartHourID = selectedStartHourID;
            busSchedule.BusSheduleDate = calendar.SelectionStart;

            if (calendar.SelectionStart.Date < DateTime.Now.Date)
            {
                MessageBox.Show("The selected date is old!", "Message",
                     MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            if (!DAO.GetDestinationWeekSchedule(currentID.First()).Contains(calendar.SelectionStart.DayOfWeek.ToString()))
            {
                MessageBox.Show("The week schedule doesn't support the selected day of week!", "Message",
                     MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else if (DAO.DoesDateExistsForThisHour(calendar.SelectionStart, lbStartHours.Text, currentID.First()))
            {
                MessageBox.Show("There is already bus in service for the selected date and start hour!", "Message",
                     MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                DAO.AttachBus(busSchedule);
                BusesForStartHourBindControls();
            }
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            frmMain main = new frmMain();
            main.Show();
            this.Hide();
        }

        private void cbDestinations_SelectedIndexChanged(object sender, EventArgs e)
        {
            StartHoursBindControls();
        }

        private void lbStartHours_SelectedIndexChanged(object sender, EventArgs e)
        {
            BusesForStartHourBindControls();
            DatesForBusBindControls();
        }

        private void lbBusesInService_SelectedIndexChanged(object sender, EventArgs e)
        {
            DatesForBusBindControls();
        }

        private void frmBusesSchedules_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

        private void frmBusesSchedules_Load(object sender, EventArgs e)
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("en-US");
        }

        private void calendar_DateChanged(object sender, DateRangeEventArgs e)
        {
            
        }

        private void btnDetachBus_Click(object sender, EventArgs e)
        {       
            string selectedDestination = cbDestinations.SelectedItem.ToString();
            var currentID = (from d in DAO.GetAllDestinations()
                            where d.DestistionName == selectedDestination
                            select d.DestinationID).First();

            bool confirm =
                MessageBox.Show("Are you sure?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question,
                 MessageBoxDefaultButton.Button1) == DialogResult.Yes;

            if (confirm)
            {
                DAO.DetachBus(lbDates.Text, lbStartHours.Text, cbBuses.SelectedValue.ToString(), currentID);
                BusesForStartHourBindControls();
                DatesForBusBindControls();
            }
        }
    }
}
